import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xindong/common/xindong_define.dart';
import 'package:xindong/models/xindong_photo_class.dart';
import 'package:xindong/res/assets_res.dart';
import 'package:xindong/routes/userspace/xindong_kongjian_ctr.dart';
import 'package:xindong/utils/xindong_image_util.dart';
import 'package:xindong/widgets/common/xindong_app_bar.dart';
import 'package:xindong/widgets/common/xindong_round_image.dart';
import 'package:xindong/widgets/image/browser/xindong_images_browser.dart';
import 'package:xindong/widgets/image/image_grid/xindong_image_grid_view.dart';

class XinDongKongJianPhotosYeMian extends StatefulWidget {
  const XinDongKongJianPhotosYeMian({super.key});

  @override
  State<XinDongKongJianPhotosYeMian> createState() => _XinDongKongJianPhotosYeMianState();
}

class _XinDongKongJianPhotosYeMianState extends State<XinDongKongJianPhotosYeMian> {
  void _onDel(XinDongPhotoClass model) {
    XinDongKongJianCtr.to.albumDelete(model);
  }

  @override
  void initState() {
    super.initState();
    XinDongKongJianCtr.to.requestUserPhotos();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: XinDongAppBar("我的相册"),
      body: GetBuilder<XinDongKongJianCtr>(
          id: XinDongKongJianCtr.updateIdLoadingPhoto,
          builder: (controller) {
            const double spacing = 8.0;
            final dataList = controller.photos;
            return XinDongImageGridView(
              dataList: dataList,
              spacing: spacing,
              columns: 4,
              maxCount: 0,
              radius: 8.0,
              padding: const EdgeInsets.all(kPaddingValue),
              onTapAddition: () => XinDongKongJianCtr.to.uploadAlbum(updatePhotoList: true),
              itemBuilder: (BuildContext context, int index) {
                final model = dataList[index];
                return GestureDetector(
                  onTap: () => XinDongImagesBrowser.show(list: dataList, currentIndex: index),
                  child: Stack(
                    children: [
                      XinDongRoundImage(url: model.picthumb.first),
                      Positioned(
                        top: -5,
                        right: -5,
                        child: IconButton(
                          icon: ImageUtil.asset(AssetsRes.ICON_CLOSE, width: 20, height: 24),
                          alignment: Alignment.topRight,
                          onPressed: () => _onDel(model),
                        ),
                      ),
                    ],
                  ),
                );
              },
            );
          }),
    );
  }
}
